"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，非交互节点异常
Create At   : 2024/10
Owner       : haomeng
Description :
    1、构造非交互节点异常，dssserver也异常
    2、进入交互模式，输出部分常规命令
    3、恢复非交互节点的dssserver进程正常，
    4、进入交互模式，输入部分常规命令
Expect      :
    1、停止节点成功
    2、执行成功
    3、恢复节点成功
    4、执行成功
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')
        self.s1_node = Node('Standby1DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.directory = 'dsscmd_file'
        self.file = 'file.txt'
        self.new_file1 = 'new_file1.txt'
        self.new_file2 = 'new_file2.txt'

    def test_dsscmd(self):
        text = '-----step1:构造非交互节点异常  expect:停止节点成功-----'
        self.log.info(text)

        stop_node = self.pri_sh.exec_cm_ctl('stop -n 2')
        self.log.info(stop_node)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop_node, '停止节点失败')

        text = '-----step2:进入交互式模式，输入部分操作   expect:执行成功-----'
        self.log.info(text)

        param_list1 = [f'mkdir -p +data -d {self.directory}',
                       f'ls -p +data/{self.directory}',
                       f'touch -p +data/{self.directory}/{self.file}',
                       f'ls -p +data/{self.directory}/{self.file}',
                       f'find -p +data -n {self.file}',
                       f'rmdir -p +data/{self.directory} -r']
        cmd = f'''dsscmd -i'''
        for param in param_list1:
            execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd1)
            res1 = self.node.sh(execute_cmd1).result()
            self.log.info(res1)
            self.assertNotIn('Failed to get uds connection', res1, '连接dss失败')

        text = '-----step3:恢复非交互节点状态   expect:恢复成功-----'
        self.log.info(text)

        start_node = self.pri_sh.exec_cm_ctl('start -n 2')
        self.log.info(start_node)
        self.assertIn(self.constant.cm_start_node_success_msg, start_node, '恢复节点失败')

        text = '-----step4:进入交互式模式，输入部分操作   expect:查看成功-----'
        self.log.info(text)

        for param in param_list1:
            execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd2)
            res2 = self.node.sh(execute_cmd2).result()
            self.log.info(res2)
            self.assertNotIn('Failed to get uds connection', res2, '连接dss失败')

    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
